<?php
namespace App\Models\Account;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use App\Admin\Extra\Search\Html;
use \Illuminate\Support\Facades\Input;

/**
 * 行为体力分析
 * 仅仅限于钻石购买体力。
 */
class BehaviorCoins extends  Model{

    protected $table='behavior_log';
    protected $_db_select = 'remote_bgsi';

    public $_max_count = 0;
    

    public function __construct()
    {
        \App\Models\Area::RemoteBgsiSelectByArea(Request::get('area'));
    }

    // 按时间为条件，购买次数分组， 主要查人数；

    public function paginate() {
        $perPage = Request::get('per_page', 100);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;     
        
        $rw = $this->selectGroup();
        self::SourceCondition($rw);
        // $rw->groupBy("op_module", "op");
        $rw->groupBy("player_id");
        $total = $rw->count();
        $rw->orderBy('op_time', "DESC");
        $rw->skip($start)->take($perPage);
        $data =  $rw->get();
        $data = $data->toArray();

        // \App\Models\Account\Behavior::attachItemWithOpAlias($data);
        $data_sta = [];
        foreach ($data as $obj) {
            $stage = self::getStageBy(Input::get('op'), $obj->cost);
            $key = $stage[0];
            $data_sta[$key]['num']++;
            $data_sta[$key]['cost'] = $stage[1];
            if (!count($data_sta[$key]['player_id'] )) {
                $data_sta[$key]['player_id'] = [];
            }
            $data_sta[$key]['player_id'][] = $obj->player_id;
        }
        ksort($data_sta);
        $total = count($data_sta);
        // $list = static::hydrate($data_sta);

        $paginator = new LengthAwarePaginator($data_sta, $total, $perPage);

        $paginator->setPath(url()->current());
        // \Log::debug(__METHOD__. print_r($data_sta, true));
        \Log::debug(__METHOD__, [$rw->toSql().'-----------------------']);
        return $paginator;
    }

    public function selectdb($select = ''){
        if ($select) {
            return $this->_db_select = $select;
        }
        return $this->_db_select;
    }
    public function selectGroup() {
        return \DB::connection($this->selectdb())->table($this->table)
        ->select('op','op', \DB::raw('count(id) as times'), \DB::raw('sum(op_val_1) as cost'), 'player_id');
    }
    public static function SourceCondition($rw) {
        $cond = Request::all();

        if ($cond['player_id']){
            $pids = explode(',', $cond['player_id']);
            $rw->whereIn("player_id", $pids);
        }
        if ($cond['op']){
            $rw->where("op", $cond['op']);
        }
        if ($cond['level']){
            $rw->where("level",'<=', $cond['level']);
        }
        if ($cond['vip']){
            $rw->where("op_val_3",'<=', $cond['vip']);
        }

        // if ($cond['area']){
        //     $rw->where("area", $cond['area']);
        // }
        if (empty($cond['op_time'])) {
            $cond['op_time'] = ['start' => time() - 30 * 86400];
        }
        if (is_array($cond['op_time'])) {
            $start = \App\ModelStatistic\StatisticCommon::GetIntTime($cond['op_time']['start']) ?: 0;
            $rw->where("op_time", ">", $start);
            $end = \App\ModelStatistic\StatisticCommon::GetIntTime($cond['op_time']['end']) ?: 0;
            if ($end) {
                $rw->where("op_time", "<", $end);
            }
        }
    }

    // 
    public static function getStageBy($op, $cost) {
        $cost = intval($cost);
        $key = 0;
        $stage_array = [];
        switch ($op) {
            case 'S.DIAMOND_REDUCE' :
            case 'S.DIAMOND_ADD':
                $stage_array = self::getDiamondStateArray();
            break;
            case 'S.GOLD_REDUCE':
            case 'S.GOLD_ADD':
                $stage_array = self::getGoldStateArray();
            break;
            case 'S.TICKET_REDUCE':
            case 'S.TICKET_ADD':
                $stage_array = self::getTicketArray();
            break;
        }
        foreach ( $stage_array as  $val) {
            if ($val > $cost) {
                break;
            }
            $key++;
        }
        if ($key == 0) {
            return [$key, '0  ~  ' . $stage_array[$key]];
        }
        if ((count($stage_array) ) <= $key) {
            return [$key, $stage_array[$key - 1] . ' ~ +oo'];
        }
        return [$key, $stage_array[$key - 1] . ' ~ ' . $stage_array[$key]];
    }
    public static function getDiamondStateArray() {
        return [200, 500, 800, 1200, 2000, 4000, 6000, 10000, 30000, 60000, 100000, 200000];
    }
    public static function getGoldStateArray() {
        return [20000, 50000, 100000, 200000, 300000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 50000000];
    }

    public static function getTicketArray() {
        return [60, 120, 360, 1280, 3280, 6480, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 100000, 150000, 200000, 500000];
    }
}